package framework.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * Class implements the JPanel to be used in displaying the controls for the
 * model. This should be added to the MainFrame.
 * 
 * @author Bill Ge, May Zhai, Alex Yoo
 */
public class ControlPanel extends JPanel implements ActionListener{
	/** final int to represent the width of the panel*/
	protected final static int CONTROLPANEL_WIDTH = MainFrame.FRAME_WIDTH;
	/** final int to represent the height of the panel to be proportional to the Main frame's height*/
	protected final static int CONTROLPANEL_HEIGHT = (int)(MainFrame.FRAME_HEIGHT * .3);
	
	/** button for the custom feature of going forward in time for the model. */
	protected JButton stepForward = null;
	
	/** button for the custom feature of going back in time for the model. */
	protected JButton stepBackward = null;
	
	/** button for pausing/playing the model. */
	protected JButton play = null;
	
	/** Timer to set the pace of the model. */
	protected Timer timer = null;
	
	/** boolean represents whether the game is paused or not. */
	protected boolean applicationPaused = false;
	
	/**
	 * Constructor sets up the JPanel such that it is ready to be added to
	 * the MainFrame.
	 */
	ControlPanel(){
		stepForward = new JButton("Forward");
		stepBackward = new JButton("Backward");
		play = new JButton("Play");
		timer = new Timer(1000, this);
		applicationPaused = true;
		addPanelObjects();
	}
	
	/**
	 * Adds all the appropriate components to the JPanel.
	 */
	protected void addPanelObjects(){
		this.add(stepForward);
		stepForward.addActionListener(this);
		this.add(stepBackward);
		stepBackward.addActionListener(this);
		this.add(play);
		play.addActionListener(this);
		
		timer.start();
	}

	/**
	 * Notify the MainFrame when the user is requesting a new grid.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Object src = e.getSource();
		
		if(src == timer && !applicationPaused){
			MainFrame.stepForward();
		}
		if(src == play){
			if(applicationPaused){
				applicationPaused = false;
				play.setText("Pause");
			}else{
				play.setText("Play");
				applicationPaused = true;
			}
		}else if(src == stepBackward){
			MainFrame.stepBackward();
		}else if(src == stepForward){
			MainFrame.stepForward();
		}
	}
}
